<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class AIController extends Controller
{
    public function processText(Request $request)
    {
        // 1. 验证输入
        $validated = $request->validate([
            'text' => 'required|string|max:1000'
        ]);

        // 2. 调用AI接口 (示例使用OpenAI)
        try {
            $response = Http::withHeaders([
                'Authorization' => 'Bearer ' . env('AI_API_KEY'),
                'Content-Type' => 'application/json'
            ])->post('https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions', [
                'model' => 'qwen-plus',
                'messages' => [['role' => 'user', 'content' => $validated['text']]]
            ]);

            // 3. 处理AI响应
            if ($response->successful()) {
                $aiResponse = $response->json()['choices'][0]['message']['content'];
                return response()->json(['result' => $aiResponse]);
            } else {
                return response()->json(['error' => 'AI服务请求失败'], 500);
            }

        } catch (\Exception $e) {
            return response()->json(['error' => $e->getMessage()], 500);
        }
    }
}